
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>如何使用 Daphne 托管 Django &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" />
    <link rel="next" title="如何使用 Hypercorn 托管 Django。" href="hypercorn.html" />
    <link rel="prev" title="如何使用 ASGI 来部署" href="index.html" />



 
<script src="../../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="如何使用 ASGI 来部署">previous</a>
     |
    <a href="../../index.html" title="操作指南" accesskey="U">up</a>
   |
    <a href="hypercorn.html" title="如何使用 Hypercorn 托管 Django。">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="howto-deployment-asgi-daphne">
            
  <div class="section" id="s-how-to-use-django-with-daphne">
<span id="how-to-use-django-with-daphne"></span><h1>如何使用 Daphne 托管 Django<a class="headerlink" href="#how-to-use-django-with-daphne" title="永久链接至标题">¶</a></h1>
<p><a class="reference external" href="https://pypi.org/project/daphne/">Daphne</a> 是一个纯Python编写的应用于UNIX环境的由Django项目维护的ASGI服务器。它扮演着ASGI参考服务器的角色。</p>
<div class="section" id="s-installing-daphne">
<span id="installing-daphne"></span><h2>安装 Daphne<a class="headerlink" href="#installing-daphne" title="永久链接至标题">¶</a></h2>
<p>你可以通过 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 来安装 Daphne</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python -m pip install daphne
</pre></div>
</div>
</div>
<div class="section" id="s-running-django-in-daphne">
<span id="running-django-in-daphne"></span><h2>在 Daphne 中运行 Django<a class="headerlink" href="#running-django-in-daphne" title="永久链接至标题">¶</a></h2>
<p>一旦 Daphne 安装完毕，你就可以使用 <code class="docutils literal notranslate"><span class="pre">daphne</span></code> 命令了，它将用来启动 Daphne 服务进程。在最简单的情形下，Daphne 加上包含一个 ASGI 应用模块的位置和应用的名称（以冒号分隔）。</p>
<p>对于一个典型的 Django 项目，可以像下面这样来启动 Daphne</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>daphne myproject.asgi:application
</pre></div>
</div>
<p>它将开启一个进程，监听 <code class="docutils literal notranslate"><span class="pre">127.0.0.1:8000</span></code>。这需要你的项目位于 Python path 上。为了确保这点，你应该在与 <code class="docutils literal notranslate"><span class="pre">manage.py</span></code> 文件相同的路径中运行这个命令。</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">如何使用 Daphne 托管 Django</a><ul>
<li><a class="reference internal" href="#installing-daphne">安装 Daphne</a></li>
<li><a class="reference internal" href="#running-django-in-daphne">在 Daphne 中运行 Django</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="index.html"
                        title="上一章">如何使用 ASGI 来部署</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="hypercorn.html"
                        title="下一章">如何使用 Hypercorn 托管 Django。</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/howto/deployment/asgi/daphne.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="如何使用 ASGI 来部署">previous</a>
     |
    <a href="../../index.html" title="操作指南" accesskey="U">up</a>
   |
    <a href="hypercorn.html" title="如何使用 Hypercorn 托管 Django。">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>